import { Controller, HttpCode, HttpStatus, MaxFileSizeValidator, ParseFilePipe, Post, UploadedFile, UploadedFiles, UseInterceptors } from '@nestjs/common';
import { UploadService } from './upload.service';
import { ApiTags } from '@nestjs/swagger';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import { UploadFileInterface } from './interfaces/file.interface';
import { Public } from '../decorators/public.decorator';

@Controller('upload')
@ApiTags("文件上传模块")
export class UploadController {
  constructor(private readonly uploadService: UploadService) { }

  @Public()
  @Post('single')
  @HttpCode(HttpStatus.OK)
  @UseInterceptors(FileInterceptor('file'))
  uploadSingle(@UploadedFile(
    new ParseFilePipe({
      validators: [
        new MaxFileSizeValidator({
          maxSize: 1024 * 1024 * 10,
          message: "文件大小不能超过10M",
        }),
      ],
    }),
  ) file: UploadFileInterface) {
    return this.uploadService.uploadSingle(file)
  }

  @Public()
  @Post("multiple")
  @HttpCode(HttpStatus.OK)
  @UseInterceptors(FilesInterceptor("file"))
  uploadMultiple(
    @UploadedFiles(
      new ParseFilePipe({
        validators: [
          new MaxFileSizeValidator({
            maxSize: 1024 * 1024 * 10,
            message: "文件大小不能超过10M",
          }),
        ],
      }),
    )
    files: UploadFileInterface[],
  ) {
    return this.uploadService.uploadMultiple(files);
  }

}

